package com.dy.图.图的遍历;

import java.util.LinkedList;
import java.util.Queue;

public class 广度优先 {
    /**
     * 队列
     *
     */
    static int vex[]={'A','B','C','D','E','F','G'};
    public static void main(String[] args) {
        int vexNum = 7;

        boolean[] visited = new boolean[vexNum];
        int arc[][]={{0,1,0,1,1,0,0},
                     {1,0,0,0,0,0,0},
                     {0,0,0,1,1,0,0},
                     {1,0,1,0,0,0,0},
                     {1,0,1,0,0,0,0},
                     {0,0,0,0,0,0,1},
                     {0,0,0,0,0,1,0}
        };
        Queue<Integer> queue = new LinkedList<>();
        for(int i=0;i<vex.length;i++){
            if(visited[i]==false ){//当前节点没访问过
                BFS(i,arc,visited);
            }
        }
    }
    static void  BFS(int i,int[][] arc,boolean visited[]){
        visited[i]=true;
        Queue<Integer> queue = new LinkedList<>();
        queue.offer(i);
        while(!queue.isEmpty()){
            int tmp = queue.poll();
            System.out.println((char)vex[tmp]);
            for(int j=0;j<visited.length;j++){
                if(arc[tmp][j]==1 &&visited[j]==false){
                    queue.add(j);
                    visited[j]=true;
                }
            }
        }
    }
}
